package com.zhongyu.controller;


import com.zhongyu.mapper.OrderMapper;
import com.zhongyu.pojo.Order;
import com.zhongyu.pojo.User;
import com.zhongyu.service.OrderService;
import com.zhongyu.vo.CgOrderVo;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
/*
采购申请
 */
@RestController
@RequestMapping("/caigou/cgsq")
public class  OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private OrderMapper orderMapper;

    @RequiresPermissions("cgsq:list")
    @GetMapping("/search/{pageNo}/{pageSize}")
    public ResultVo search(@PathVariable Integer pageNo, @PathVariable Integer pageSize, CgOrderVo cgOrderVo) {
        PageResult pageResult = orderService.search(pageNo, pageSize, cgOrderVo);
        return ResultVo.success(pageResult);

    }

    @PostMapping("/update")

    public ResultVo update(@RequestBody Order order, HttpSession session) {
        try {
            List<Order> order1 = orderMapper.findAllOrder(order.getId());
            Order order2 = order1.get(0);
            order2.setCheckTime(LocalDateTime.now());
            order2.setState("1");
            order2.setStat("7");
            User loginUser = (User) session.getAttribute("loginUser");
            order2.setChecker(loginUser.getId());
            orderService.update(order2);
            return ResultVo.success("审核成功!");
        } catch (Exception ex) {

            return ResultVo.failure("审核失败!", ex);
        }
    }

    /*审核驳回功能*/
    @PostMapping("/updateBoHui")

    public ResultVo updatBoHuie(@RequestBody Order order) {
        try {
            List<Order> order1 = orderMapper.findAllOrder(order.getId());
            Order order2 = order1.get(0);
            order2.setWayBillCode(order.getWayBillCode());
            order2.setStat("4");
            order2.setState("0");
            orderService.update(order2);
            return ResultVo.success("驳回成功!");
        } catch (Exception ex) {

            return ResultVo.failure("驳回失败!", ex);
        }
    }

    @PostMapping("/updateQue")

    public ResultVo updateueQue(@RequestBody Order order, HttpSession session) {
        try {
            List<Order> order1 = orderMapper.findAllOrder(order.getId());
            Order order2 = order1.get(0);
            order2.setPurchaseTime(LocalDateTime.now());
            order2.setState("2");
            order2.setStat("7");
            User loginUser = (User) session.getAttribute("loginUser");
            order2.setPurchaser(loginUser.getId());
            orderService.update(order2);
            return ResultVo.success("确认成功!");
        } catch (Exception ex) {

            return ResultVo.failure("确认失败!", ex);
        }
    }
    /*确认驳回功能*/
    @PostMapping("/updateQueBoHui")

    public ResultVo updatQueBoHuie(@RequestBody Order order) {
        try {
            List<Order> order1 = orderMapper.findAllOrder(order.getId());
            Order order2 = order1.get(0);
            order2.setWayBillCode(order.getWayBillCode());
            order2.setStat("5");
            order2.setState("1");
            orderService.update(order2);
            return ResultVo.success("驳回成功!");
        } catch (Exception ex) {

            return ResultVo.failure("驳回失败!", ex);
        }
    }

    @PostMapping("/updateRu")

    public ResultVo updateueRu(@RequestBody Order order, HttpSession session) {
        try {
            List<Order> order1 = orderMapper.findAllOrder(order.getId());
            Order order2 = order1.get(0);
            order2.setStockTime(LocalDateTime.now());
            order2.setState("3");
            User loginUser = (User) session.getAttribute("loginUser");
            order2.setStockKeeper(loginUser.getId());
            orderService.update(order2);
            return ResultVo.success("入库成功!");
        } catch (Exception ex) {

            return ResultVo.failure("入库失败!", ex);
        }
    }
    /*确认驳回功能*/
    @PostMapping("/updateRuBoHui")

    public ResultVo updatRuBoHuie(@RequestBody Order order) {
        try {
            List<Order> order1 = orderMapper.findAllOrder(order.getId());
            Order order2 = order1.get(0);
            order2.setWayBillCode(order.getWayBillCode());
            order2.setStat("5");
            order2.setState("1");
            orderService.update(order2);
            return ResultVo.success("驳回成功!");
        } catch (Exception ex) {

            return ResultVo.failure("驳回失败!", ex);
        }
    }
}